package cn.xz.zgsg.dialog

import android.view.Gravity
import android.view.View
import android.widget.EditText
import android.widget.ImageView
import android.widget.TextView
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import cn.xz.zgsg.R
import cn.xz.zgsg.adapter.LocalListAdapter
import cn.xz.zgsg.adapter.U3DTAdapter
import cn.xz.zgsg.base.BaseApplication
import cn.xz.zgsg.base.BaseDialogRxFragment
import cn.xz.zgsg.bean.DeviceList
import cn.xz.zgsg.bean.U3dWelListBean
import cn.xz.zgsg.util.ToastUtil
import cn.xz.zgsg.util.U3dHttp
import cn.xz.zgsg.util.gone
import cn.xz.zgsg.util.visible
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.listener.OnItemClickListener
import kotlin.collections.ArrayList

/**
 * 操作欢迎词弹窗
 * */
class AddU3DWelComeDialog: BaseDialogRxFragment() {

    private var mClickListener: OnClickListener? = null

    private var textSize: Int = 120
    private var text: String = "请输入文字"
    private var font: String = ""
    private var mDevice: DeviceList? = null

    private var hasSize = true

    private var tv_t: TextView? = null
    private var tvOk: TextView? = null
    private var tvCancel: TextView? = null
    private var tvWelSize: TextView? = null
    private var tvTitle: TextView? = null
    private var tvFont: TextView? = null
    private var ivAddWel: ImageView? = null
    private var ivJWel: ImageView? = null
    private var etDialogEdit: EditText? = null
    private var viewM: View? = null
    private var rvFont: RecyclerView? = null

    companion object {
        fun newInstance(
            hasSize: Boolean,
            textSize: Int,
            text: String,
            font: String,
            mDevice: DeviceList,
            clickListener: OnClickListener
        ): AddU3DWelComeDialog {
            val dialog = AddU3DWelComeDialog()
            dialog.hasSize = hasSize
            dialog.textSize = textSize
            dialog.text = text
            dialog.font = font
            dialog.mDevice = mDevice
            dialog.mClickListener = clickListener
            return dialog
        }
    }

    override fun getLayoutId(): Int {
        return R.layout.dialog_u3d_add_welcome
    }

    override fun configDialog() {
        this.setDialogSize(gravity = Gravity.CENTER,width = 500)
    }

    override fun initViews() {
        dialog?.let {
            tvOk = it.findViewById(R.id.tv_ok)
            tv_t = it.findViewById(R.id.tv_t)
            tvCancel = it.findViewById(R.id.tv_cancel)
            tvWelSize = it.findViewById(R.id.tv_wel_size)
            ivAddWel = it.findViewById(R.id.iv_add_wel)
            ivJWel = it.findViewById(R.id.iv_j_wel)
            etDialogEdit = it.findViewById(R.id.et_dialog_edit)
            tvTitle = it.findViewById(R.id.tv_title)
            tvFont = it.findViewById(R.id.tv_font)
            viewM = it.findViewById(R.id.view_m)
            rvFont = it.findViewById(R.id.rv_font)
        }

        //点击外部不隐藏
        dialog?.setCancelable(false)
        dialog?.setCanceledOnTouchOutside(false)


        if (hasSize){
            tv_t?.visible()
            tvWelSize?.visible()
            ivAddWel?.visible()
            ivJWel?.visible()
        }else{
            tv_t?.gone()
            tvWelSize?.gone()
            ivAddWel?.gone()
            ivJWel?.gone()
        }
        //数据
        initData()
        //点击事件
        initEvent()
    }

    private fun initData(){
        if (tvWelSize != null){
            tvWelSize!!.text = textSize.toString()
        }
        if (etDialogEdit != null){
            if (text.contains("请输入文字")){
                tvTitle?.text = "请输入需要添加的欢迎词"
                etDialogEdit!!.hint = text
            }else{
                tvTitle?.text = "编辑当前欢迎词"
                etDialogEdit!!.setText(text)
            }
        }
        if (font.isNotEmpty()){
            tvFont!!.text = font.toString()
        }
        //获取字体包样式
        if (mDevice != null){
            U3dHttp.getU3Font(mDevice!!.ip,object :U3dHttp.GetU3FontListener{
                override fun getData(mList: ArrayList<String>?) {
                    tvFont!!.post {
                        if (mList != null && mList.size > 0){
                            if (font.isEmpty()){
                                tvFont!!.text = mList.get(0)
                            }
                            rvFont?.layoutManager = LinearLayoutManager(BaseApplication.getContext())
                            val mAdapter = U3DTAdapter()
                            rvFont?.adapter = mAdapter
                            mAdapter.setList(mList)
                            mAdapter.setOnItemClickListener(object: OnItemClickListener {
                                override fun onItemClick(
                                    adapter: BaseQuickAdapter<*, *>,
                                    view: View,
                                    position: Int
                                ) {
                                    tvFont!!.text = mList[position]
                                    viewM?.gone()
                                    rvFont?.gone()
                                }

                            })
                        }
                    }
                }

            })
        }
    }

    private fun initEvent(){
        tvOk?.setOnClickListener {
            if (tvWelSize!= null && etDialogEdit != null && etDialogEdit!!.text.toString().isNotEmpty()){
                mClickListener?.onYes(Integer.parseInt(tvWelSize!!.text.toString()),etDialogEdit!!.text.toString(),tvFont!!.text.toString())
                dismiss()
            }else{
                ToastUtil.show("内容为空")
            }
        }
        tvCancel?.setOnClickListener {
            dismiss()
        }
        ivAddWel?.setOnClickListener {
            val mS = Integer.parseInt(tvWelSize!!.text.toString())
            tvWelSize?.text = (mS + 10).toString()
        }
        ivJWel?.setOnClickListener {
            val mS = Integer.parseInt(tvWelSize!!.text.toString())
            if (mS > 10){
                tvWelSize?.text = (mS - 10).toString()
            }
        }
        tvFont?.setOnClickListener {
            viewM?.visible()
            rvFont?.visible()
        }
        viewM?.setOnClickListener {
            viewM?.gone()
            rvFont?.gone()
        }
    }


    interface OnClickListener {
        fun onYes(i: Int,s: String,f: String)
    }

}